#!/bin/bash

# 原始键的前缀
src_prefix="/Config/pro/ai_call_system"
# 目标键的前缀
dst_prefix="/Config/dev/ai_call_system"

# 获取原始前缀下的所有键
keys=$(etcdctl get "$src_prefix" --prefix --keys-only)

# 循环遍历每个键
echo "$keys" | while read -r key; do
    if [ -z "$key" ]; then
        # 忽略空行
        continue
    fi

    # 去除原始前缀，获得相对键路径
    relative_key=${key#$src_prefix}
    # 读取原始键的值
    value=$(etcdctl get "$key" --print-value-only)

    if [ -z "$value" ]; then
        echo "Warning: No value found for $key, skipping..."
        continue
    fi

    # 构造新的目标键路径
    new_key="$dst_prefix$relative_key"
    # 将值复制到新的键
    etcdctl put "$new_key" "$value" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Copied $key to $new_key"
    else
        echo "Error copying $key to $new_key"
    fi
done
